{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 10\n",
    "b = 10\n",
    "a==b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 浅拷贝，是指重新分配一块内存，创建一个新的对象，里面的元素是原对象中子对象的引用\n",
    "# 深拷贝，深度拷贝，是指重新分配一块内存，创建一个新的对象，并且将原对象中的元素，以递归的方式，通过创建新的子对象拷贝到新对象中。因此，新对象和原对象没有任何关联。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140713894824664\n",
      "140713894824664\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(id(a))\n",
    "print(id(b))\n",
    "print(a is b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "t1 = (1,2,[3,4])\n",
    "t2 = (1,2,[3,4])\n",
    "print(t1==t2)\n",
    "print(t1 is t2)\n",
    "t1[-1].append(5)\n",
    "print(t1==t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "l1 = [1,2,3]\n",
    "l2 = copy.copy(l1)\n",
    "print(l1 is l2)\n",
    "print(l1==l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = (1,2,3)\n",
    "t2 = tuple(t1)\n",
    "\n",
    "t1 == t2\n",
    "\n",
    "t1 is t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], (30, 40, 50, 60), 100]\n",
      "[[1, 2, 3], (30, 40)]\n"
     ]
    }
   ],
   "source": [
    "# 浅拷贝的副作用\n",
    "l1 = [[1,2],(30,40)]\n",
    "l2 = list(l1)\n",
    "l1.append(100)\n",
    "l1[0].append(3)\n",
    "l1[1] += (50,60)\n",
    "print(l1)\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], (30, 40, 50, 60), 100]\n",
      "[[1, 2], (30, 40)]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "# 深拷贝，引用的原子对象是互相独立，不干扰的\n",
    "l1 = [[1,2],(30,40)]\n",
    "l2 = copy.deepcopy(l1)\n",
    "l1.append(100)\n",
    "l1[0].append(3)\n",
    "l1[1] += (50,60)\n",
    "print(l1)\n",
    "print(l2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
